<template>
  <div :loading="listLoading">
    <van-tabs v-model="active" sticky >
      <van-tab title="基本信息" style="margin: 15px;">
        <project-info @commit="update" v-model="product"></project-info>
      </van-tab>

      <van-tab title="图片" v-if="product.id">
        <project-image @commit="update" v-model="product"></project-image>
      </van-tab>
      <van-tab title="活动" v-if="product.id">
        <project-active @commit="update" v-model="product"></project-active>
      </van-tab>
      <van-tab title="详情" v-if="product.id">
        <projectWeb @commit="update" v-model="product"></projectWeb>
      </van-tab>

      <van-tab title="库存" v-if="product.id">
        <project-stock @commit="update" v-model="product"></project-stock>
      </van-tab>
    </van-tabs>
  </div>
</template>
<script>
import projectInfo from "./components/projectInfo";
import projectImage from "./components/projectImage";
import projectActive from "./components/projectActive";
import projectWeb from "./components/projectWeb";
import projectStock from "./components/projectStock";
import { productinfo } from "@/api/product";
import {
  creatProduct,
  updateProduct,

} from "@/api/product";

export default {
  components: {
    projectInfo,
    projectImage,
    projectActive,
    projectWeb,
    projectStock
  },
  name: "product",
  data() {
    return {
      active: 0,
      product: {},
      listLoading: false
    };
  },
  created() {
    if (this.$route.query.id) {
      this.getproductInfo()
    }
  },
  watch: {},
  filters: {},
  methods: {
    update(value){
     
      if (value.id == null) {
        this.listLoading = true;
        creatProduct(value).then(response => {
           alert(response.data.id)
          this.listLoading = false;
          this.product.id = response.data.id;
          this.$forceUpdate()
          this.$toast('保存成功');
     
        });
      } else {
        this.listLoading = true;
        updateProduct(value.id, value).then(response => {
          this.listLoading = false;
          this.product = value;
          this.getproductInfo()
          this.$toast('保存成功');
        });
      }
    },
    getproductInfo() {
      this.listLoading = true;
      productinfo(this.$route.query.id).then(response => {
        this.listLoading = false;
        this.product = response.data;
        this.$forceUpdate()
      });
    },
    on_click(step) {
      this.active = step;
    }
  }
};
</script>
<style scoped>
</style>


